Check if a given Binary Tree is SumTree
2014-06-30 21:35
701 查看
package tree;
class Ress{
int sum;
}
public class SumTree {
/**
* Following is an example of SumTree.
26
/ \
10 3
/ \ \
4 6 3
* 本质为后序遍历
* @param args
*/
public static int sum = 0;
public static boolean issumtree(TreeNode root,Ress res){
if(root==null) return true;
if(root.left==null&&root.right==null){
res.sum = root.value;
return true;
}
Ress left = new Ress();
Ress right = new Ress();
if(issumtree(root.left, left)&&issumtree(root.right, right)){
if(root.value==left.sum+right.sum){
res.sum = root.value*2;
return true;
}
return false;
}
return false;
}
public static void main(String[] args) {
TreeNode root = new TreeNode(26);
root.left = new TreeNode(10);
root.right = new TreeNode(31);
root.right.right = new TreeNode(3);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(6);
Ress res = new Ress();
System.out.println(issumtree(root, res));
}
}
class Ress{
int sum;
}
public class SumTree {
/**
* Following is an example of SumTree.
26
/ \
10 3
/ \ \
4 6 3
* 本质为后序遍历
* @param args
*/
public static int sum = 0;
public static boolean issumtree(TreeNode root,Ress res){
if(root==null) return true;
if(root.left==null&&root.right==null){
res.sum = root.value;
return true;
}
Ress left = new Ress();
Ress right = new Ress();
if(issumtree(root.left, left)&&issumtree(root.right, right)){
if(root.value==left.sum+right.sum){
res.sum = root.value*2;
return true;
}
return false;
}
return false;
}
public static void main(String[] args) {
TreeNode root = new TreeNode(26);
root.left = new TreeNode(10);
root.right = new TreeNode(31);
root.right.right = new TreeNode(3);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(6);
Ress res = new Ress();
System.out.println(issumtree(root, res));
}
}
相关文章推荐
- Data Structure Binary Tree: Check if a given Binary Tree is SumTree
- Check if the given binary tree is BST or not.
- Check whether a given Binary Tree is Complete or not 解答
- Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree andsum =
- A program to check if a binary tree is BST or not
- LeetCode题目笔记(四) -- Check if a binary tree is balanced
- [面试题]Amazon: Given two binary trees,if the first tree is subtree of the second one
- 是否是二叉搜索树 check if it is binary search tree
- 【Leetcode】Given a binary tree, check whether it is a mirror of itself
- Check if a binary tree is subtree of another binary tree
- ch4.7: given 2 huge Binary tree t1, t2(>1millon nodes), decide if one tree is subtree of another one
- Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never diffe
- (SOJ) check if a word is on a given Boggle board
- Data Structure Binary Tree: Convert a given tree to its Sum Tree
- 判断是否为完全二叉树 Check whether a binary tree is a complete tree or not
- Q4.1 Implement a function to check if a tree is balanced
- Given two binary trees, write a function to check if they are equal or not.
- Data Structure Binary Tree: Check for Children Sum Property in a Binary Tree
- Vertical Sum in a given Binary Tree
- Check if tree b is part of tree a JAVA 实现