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;
        }
  • 相关阅读:
    ActiveMQ持久化机制
    ActiveMQ的使用
    ActiveMQ解释
    Linux CentOS安装Tomcat
    nginx使用Keepalived
    Session共享解决方案
    Spring框架AOP使用扩展
    Myabtis测试(二)错题整理分析
    初识Spring及打印机案例
    MyBatis注解
  • 原文地址:https://www.cnblogs.com/skillking/p/9735262.html
Copyright © 2011-2022 走看看