前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Leetcode】129.求根到叶子节点数字之和

【Leetcode】129.求根到叶子节点数字之和

作者头像
Leetcode名企之路
发布2019-05-27 20:23:38
3160
发布2019-05-27 20:23:38
举报
文章被收录于专栏:Leetcode名企之路Leetcode名企之路

题目

给定一个二叉树,它的每个结点都存放一个 0-9 的数字,每条从根到叶子节点的路径都代表一个数字。

例如,从根到叶子节点路径 1->2->3 代表数字 123

计算从根到叶子节点生成的所有数字之和。

说明: 叶子节点是指没有子节点的节点。

示例 1:

代码语言:javascript
复制
输入: [1,2,3]
    1
   / \
  2   3
输出: 25
解释:
从根到叶子节点路径 1->2 代表数字 12.
从根到叶子节点路径 1->3 代表数字 13.
因此,数字总和 = 12 + 13 = 25.

示例 2:

代码语言:javascript
复制
输入: [4,9,0,5,1]
    4
   / \
  9   0
 / \
5   1
输出: 1026
解释:
从根到叶子节点路径 4->9->5 代表数字 495.
从根到叶子节点路径 4->9->1 代表数字 491.
从根到叶子节点路径 4->0 代表数字 40.
因此,数字总和 = 495 + 491 + 40 = 1026.

题解

二叉树的题目我们首先想到的就是递归求解。递归的方式很简单,用先序遍历的变形。

  1. 先遍历根节点;
  2. 遍历左子树,遍历左子树的时候,把走当前路径的数字带到左子树的求解中;
  3. 遍历右子树,遍历右子树的时候,把走当前路径的数字带到右子树的求解中;
  4. 更新总的和。
代码语言:javascript
复制
class Solution {
    private int sum = 0;
    private void helper(TreeNode node, int father) {
        if (node == null) return ;
        int current = father * 10 + node.val;
        if (node.left == null && node.right == null) {
            sum += current;
            return;
        }
        helper(node.left, current);
        helper(node.right, current);
    }

    public int sumNumbers(TreeNode root) {
        if (root == null) return sum;
        helper(root, 0);
        return sum;
    }
}

通常还可以用stack的思路来解递归的题目。先序非递归的代码我们知道是用stack来保存遍历过的元素。而因为本题要记录到叶节点的数字,所以需要一个额外的stack来记录数字。每次出stack之后,如果是叶子节点,那么加和;如果不是,那么就看左右子树,入stack。

代码语言:javascript
复制
class Solution {
    public int sumNumbers(TreeNode root) {
        int sum = 0;
        if (root == null) return sum;
        Stack<TreeNode> nodeStack = new Stack<>();
        Stack<Integer> numStack = new Stack<>();
        nodeStack.add(root);
        numStack.add(0);
        while (!nodeStack.isEmpty()) {
            TreeNode current = nodeStack.pop();
            Integer currentNum = numStack.pop() * 10 + current.val;
            if (current.left == null && current.right == null) {
                sum += currentNum;
            }
            if (current.left != null) {
                nodeStack.add(current.left);
                numStack.add(currentNum);
            }
            if (current.right != null) {
                nodeStack.add(current.right);
                numStack.add(currentNum);
            }
        } 
        return sum;
    }
}

其实,我们可以看到,最关键的是找到叶子节点,然后加和这个操作。叶子节点我们同样可以用层序遍历的方式来解这道题目。层序遍历用队列来解。

代码语言:javascript
复制
public class Solution {
    public int sumNumbers(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        Queue<Integer> numQueue = new LinkedList<Integer>();
        if(root == null) return 0;
        int res = 0;
        queue.add(root);
        numQueue.add(0);
        while(!queue.isEmpty()) {
            int size = queue.size();
            // 把该层的都入队,同时如果遇到叶节点,计算更新
            while(size-- > 0) {
                root = queue.poll();
                int val = numQueue.poll() * 10 + root.val;
                if(root.left == null && root.right == null)
                    res += val;
                if(root.left != null) {
                    queue.add(root.left);
                    numQueue.add(val);
                }
                if (root.right != null) {
                    queue.add(root.right);
                    numQueue.add(val);
                }
            }
        }
        return res;
    }
}

总结,二叉树的题目,大多数都是遍历的变形,面试时候看用bfs,还是dfs,一般来说很快就能得出答案。写非递归代码的时候,注意判断一下非空,不要把null节点入队或者入栈。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-05-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Leetcode名企之路 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目
  • 题解
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档