zoukankan      html  css  js  c++  java
  • 112. Path Sum

    一、题目

      1、审题

      

      2、分析

        判断所给二叉树是否存在一条从根节点到叶子节点的所有节点值之和为 sum。

    二、解答

      1、思路:

        方法一、

          采用递归的方式进行判断。

        public boolean hasPathSum(TreeNode root, int sum) {
            
            if(root == null)
                return false;
            if(root.val == sum && root.left == null && root.right == null)
                return true;
            
            return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
            
        }

      方法二、

        采用 preOrder 的迭代方式进行 DFS 二叉树,若找到, 返回 true。

    public boolean hasPathSum(TreeNode root, int sum) {
            if(root == null)
                return false;
            
            Stack<TreeNode> nodeStack = new Stack<>();
            Stack<Integer> valStack = new Stack<>();
            nodeStack.push(root);
            valStack.push(root.val);
            
            while(!nodeStack.isEmpty()) {
                TreeNode node = nodeStack.pop();
                int value = valStack.pop();
                
                if(node.left == null && node.right == null && value == sum)
                    return true;
                else {
                    if(node.right != null) {
                        nodeStack.push(node.right);
                        valStack.push(node.right.val + value);
                    }
                    
                    if(node.left != null) {
                        nodeStack.push(node.left);
                        valStack.push(node.left.val + value);
                    }
                }
            }
            return false;
        }

      方法三、

        采用 postOrder 的迭代方式查找是否存在符合的子树。

        public boolean hasPathSum(TreeNode root, int sum) {
            Stack<TreeNode> stack = new Stack<>();
            TreeNode pre = null;    // 记录当前节点的右孩子是否访问过
            TreeNode cur = root;    // 当前访问的节点
            int target = 0;
            // postOrder
            while(cur != null || !stack.isEmpty()) {
                
                while(cur != null) {
                    stack.push(cur);
                    target += cur.val;
                    cur = cur.left;
                }
    
                cur = stack.peek();         // 未出栈
                if(cur.left == null && cur.right == null && sum == target)
                    return true;
                
                if(cur.right != null && pre != cur.right) {
                    cur = cur.right;    // cur 有右孩子,且, pre 不是 cur 的右孩子。
                }
                else {        // cur 节点访问完毕
                    pre = cur;    
                    stack.pop();
                    target -= cur.val;
                    cur = null;    // 
                }
            }
            return false;
        }
  • 相关阅读:
    android项目启动应用,卸载应用,分享
    android项目复杂的listview
    android项目获得手机里所有的应用程序
    android项目获取指定目录下可用空间
    android项目实现电话自动挂断的功能
    android项目浮窗的移动
    android项目双击或者多击的实现
    C#设计模式之工厂方法与简单工厂
    C#二分查找法与拉格朗日查找法
    C#快速排序算法
  • 原文地址:https://www.cnblogs.com/skillking/p/9735262.html
Copyright © 2011-2022 走看看