/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/classSolution{publicbooleanisUnivalTree(TreeNoderoot){booleanresult=preOrderByStack(root);returnresult;}publicbooleanpreOrderByStack(TreeNoderoot){intvalue=root.val;booleanresult=true;Stack<TreeNode>stack=newStack<TreeNode>();TreeNodecurrent=root;while(current!=null||!stack.isEmpty()){while(current!=null){stack.push(current);if(current.val!=value)returnfalse;current=current.left;}if(!stack.isEmpty()){current=stack.pop();current=current.right;}}System.out.println();returntrue;}}