【LeetCode】Path Sum ---------LeetCode java 小结

时间:2022-05-26 18:42:33

Path Sum

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 and sum = 22,

              5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1

return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.

思路:深度遍历(后续遍历)。每当访问到叶子节点时,计算栈内的元素,也就是从根节点到叶子节点的路径。

public boolean hasPathSum(TreeNode root, int sum) {
Stack<TreeNode> stack = new Stack<>();
int total = 0;
if (root == null)
return false;
TreeNode qNode=root;
while (root != null) {
while (root.left != null) {
stack.push(root);
total += root.val;
root = root.left;
}
while (root != null && (root.right == null || root.right == qNode)) {
if(root.right==null&&root.left==null){//访问到叶子节点
if(total+root.val==sum){
return true;
}
}
qNode = root;// 记录上一个已输出节点
if (stack.empty())
return false;
root = stack.pop();
total-=root.val;
}
stack.push(root);
total+=root.val;
root = root.right;
}
return false;
}

这个题目中,叶子结点不是非常清楚,考虑这种情况

        1

       / 

      2  

 结果为1,应该是正确的,存在path。

在此回顾一下二叉树的遍历(参考http://maizi2011.iteye.com/blog/938749

  /** 递归实现前序遍历 */
  protected static void preorder(BTNode p) {
    if (p != null) {
      visit(p);
      preorder(p.getLeft());
      preorder(p.getRight());
    }
  }
  /** 递归实现中序遍历 */
  protected static void inorder(BTNode p) {
    if (p != null) {
      inorder(p.getLeft());
      visit(p);
      inorder(p.getRight());
    }
  }
  /** 递归实现后序遍历 */
  protected static void postorder(BTNode p) {
    if (p != null) {
      postorder(p.getLeft());
      postorder(p.getRight());
      visit(p);
    }
  }
  /** 非递归实现前序遍历 */
  protected static void iterativePreorder(BTNode p) {
    Stack<BTNode> stack = new Stack<BTNode>();
    if (p != null) {
      stack.push(p);
      while (!stack.empty()) {
        p = stack.pop();
        visit(p);
        if (p.getRight() != null)
          stack.push(p.getRight());
        if (p.getLeft() != null)
          stack.push(p.getLeft());
      }
    }
  }
  /** 非递归实现后序遍历 */
  protected static void iterativePostorder(BTNode p) {
    BTNode q = p;
    Stack<BTNode> stack = new Stack<BTNode>();
    while (p != null) {
      // 左子树入栈
      for (; p.getLeft() != null; p = p.getLeft())
        stack.push(p);
      // 当前节点无右子或右子已经输出
      while (p != null && (p.getRight() == null || p.getRight() == q)) {
        visit(p);
        q = p;// 记录上一个已输出节点
        if (stack.empty())
          return;
        p = stack.pop();
      }
      // 处理右子
      stack.push(p);
      p = p.getRight();
    }
  }
  /** 非递归实现中序遍历 */
  protected static void iterativeInorder(BTNode p) {
    Stack<BTNode> stack = new Stack<BTNode>();
    while (p != null) {
      while (p != null) {
        if (p.getRight() != null)
          stack.push(p.getRight());// 当前节点右子入栈
        stack.push(p);// 当前节点入栈
        p = p.getLeft();
      }
      p = stack.pop();
      while (!stack.empty() && p.getRight() == null) {
        visit(p);
        p = stack.pop();
      }
      visit(p);
      if (!stack.empty())
        p = stack.pop();
      else
        p = null;
    }
  }