我的错误代码
理递归思路
/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */ public class Solution { /** * @param root the root of binary tree * @return the root of the minimum subtree */ public TreeNode findSubtree(TreeNode root) { // Write your code here TreeNode result = new TreeNode(0); int min=0; if (root == null) { return null; } findSum(root, result, min); return result; } private int findSum(TreeNode root, TreeNode result, int min) { if (root == null) { return 0; } int sum; int left = findSum(root.left, result, min); int right = findSum(root.right, result, min); sum = root.val + left + right; if (root.val + left + right > left && left <= right) { min = left; result = root.left; } else if (root.val + left + right > right && left >= right) { min = right; result = root.right; } else { min = root.val + left + right; result = root; } return sum; } }
改了一点点,增加了实例变量,balance不能放在形参里面。
/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */ public class Solution { /** * @param root: The root of binary tree. * @return: True if this Binary tree is Balanced, or false. */ private boolean balance; public boolean isBalanced(TreeNode root) { // write your code here balance = true; height(root); return balance; } private int height(TreeNode root) { int h = 0; if (root == null) { return h; } int left = height(root.left); int right = height(root.right); /* if (((left - right) > 1) || ((right - left) > 1)) { balance = false; } */ if (Math.abs(right - left) > 1) { balance = false; } if (left >= right) { h = left + 1; } else { h = right + 1; } return h; } }
九章
// version 1 : traverse + divide conquer public class Solution { private TreeNode subtree = null; private int subtreeSum = Integer.MAX_VALUE; /** * @param root the root of binary tree * @return the root of the minimum subtree */ public TreeNode findSubtree(TreeNode root) { helper(root); return subtree; } private int helper(TreeNode root) { if (root == null) { return 0; } int sum = helper(root.left) + helper(root.right) + root.val; if (sum < subtreeSum) { subtreeSum = sum; subtree = root; } return sum; } } // version 2: Pure divide conquer class ResultType { public TreeNode minSubtree; public int sum, minSum; public ResultType(TreeNode minSubtree, int minSum, int sum) { this.minSubtree = minSubtree; this.minSum = minSum; this.sum = sum; } } public class Solution { /** * @param root the root of binary tree * @return the root of the minimum subtree */ public TreeNode findSubtree(TreeNode root) { ResultType result = helper(root); return result.minSubtree; } public ResultType helper(TreeNode node) { if (node == null) { return new ResultType(null, Integer.MAX_VALUE, 0); } ResultType leftResult = helper(node.left); ResultType rightResult = helper(node.right); ResultType result = new ResultType( node, leftResult.sum + rightResult.sum + node.val, leftResult.sum + rightResult.sum + node.val ); if (leftResult.minSum < result.minSum) { result.minSum = leftResult.minSum; result.minSubtree = leftResult.minSubtree; } if (rightResult.minSum < result.minSum) { result.minSum = rightResult.minSum; result.minSubtree = rightResult.minSubtree; } return result; } }