zoukankan      html  css  js  c++  java
  • 5 Binary Tree & Treebased DFS

    900. Closest Binary Search Tree Value

    https://www.lintcode.com/problem/closest-binary-search-tree-value/description?_from=ladder&&fromId=1

    1. 非递归方法:求BST中跟target最近的数字。我们先设置一个min = root.val, 然后用iterative的方法尝试更新min,然后比较target与root的大小,进行二分查找。

    public int closestValue(TreeNode root, double target) {
            // write your code here
           int min = root.val;
            while(root != null) {
                min = Math.abs(target - root.val) < Math.abs(target - min) ? root.val : min;
                root = root.val > target ? root.left : root.right;
            }
            return min;
        }

    2. 递归

      1. 比较target和root.val, => 求child是为了递归

      2. if(child == null) return root.val;

      3. 求 childClosest = closestValue(child, target)

      4. 比较 root.val 和childClosest

    public int closestValue(TreeNode root, double target) {
            // write your code here
            TreeNode child = root.val > target ? root.left : root.right;
            if(child == null) {
                return root.val;
            }
            int childClosest = closestValue(child, target);
            return Math.abs(root.val - target) > Math.abs(childClosest - target) ? childClosest : root.val;
        }

    596. Minimum Subtree

    https://www.lintcode.com/problem/minimum-subtree/description?_from=ladder&&fromId=1

    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
            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;
        }
    }
    public class Solution {
        /**
         * @param root: the root of binary tree
         * @return: the root of the minimum subtree
         */
        int subtreeSum = Integer.MAX_VALUE;
        TreeNode subtree = null;
        
        public TreeNode findSubtree(TreeNode root) {
            // write your code here
            if(root ==  null) {
                return null;
            }
            helper(root);
            return subtree;
        }
        
        public 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;
        }
    }
  • 相关阅读:
    程序员书单合集,持续整理中
    informatica9.5.1后最一步出错(ICMD_10033,INFACMD_10053)
    Informatica9.5.1配置域名错误(ICMD_10033,INFASETUP_10002,RSVCSHARED_00021)
    程序员书单_UML篇
    程序员书单_J2EE专题
    程序员书单_求职面试
    程序员书单_java专项进阶篇
    程序员书单_HTML篇
    程序员书单_数据结构和算法篇
    程序员书单_HeadFirst系列
  • 原文地址:https://www.cnblogs.com/jenna/p/10804263.html
Copyright © 2011-2022 走看看