zoukankan      html  css  js  c++  java
  • [leetCode]129. 求根到叶子节点数字之和

    csdn: https://blog.csdn.net/renweiyi1487/article/details/109351021

    题目

    链接:https://leetcode-cn.com/problems/sum-root-to-leaf-numbers

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

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

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

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

    示例 1:
    
    输入: [1,2,3]
        1
       / 
      2   3
    输出: 25
    解释:
    从根到叶子节点路径 1->2 代表数字 12.
    从根到叶子节点路径 1->3 代表数字 13.
    因此,数字总和 = 12 + 13 = 25.
    示例 2:
    
    输入: [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.
    

    dfs

    1. 确定递归函数返回值与函数参数
      由于最后要得到根节点到所有叶子节点的数字之和所以返回int;由于要进行递归,并记录路径上的数字所以函数参数为int traverse(TreeNode root, int pathVal)

    2. 确定递归退出条件
      到达叶子节点后说明完成了一条路径,这时返回这条路径上数字组成的值

    3. 编写单层逻辑
      采用线序遍历的方式记录路径上的数字

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public int sumNumbers(TreeNode root) {
            if (root == null) return 0;
            return traverse(root, 0);
        }
    
        private int traverse(TreeNode root, int pathVal) {
            if (root.left == null && root.right == null) {
                return pathVal * 10 + root.val;
            }
    
            pathVal = pathVal * 10 + root.val;
            
            int leftVal = 0;
            int rightVal = 0;
            if (root.left != null)
                leftVal = traverse(root.left, pathVal);
            if (root.right != null)
                rightVal = traverse (root.right, pathVal);
            return leftVal + rightVal;
        }
    }
    

    bfs

    维护两个队列一个队列存储节点,另一个队列存储每个节点对应的数字。
    每次从队列中取出一个节点:

    • 如果该节点不是叶子节点则判断该结点是否有左子节点与右子节点,将其子节点及对应的值加入队列中。
    • 如果取出的节点为叶子节点则将该节点对应的值进行累加。
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public int sumNumbers(TreeNode root) {
            if (root == null) return 0;
            int sum = 0;
            Queue<TreeNode> queue = new LinkedList<>();
            Queue<Integer> numQueue = new LinkedList<>();
            queue.offer(root);
            numQueue.offer(root.val);
            while (!queue.isEmpty()) {
                TreeNode cur = queue.poll();
                Integer pathVal = numQueue.poll();
                if (cur.left == null && cur.right == null) {
                    sum += pathVal;
                }
                if (cur.left != null){
                    queue.offer(cur.left);
                    numQueue.offer(cur.left.val + pathVal * 10);
                }
                    
                if (cur.right != null) {
                    queue.offer(cur.right);
                    numQueue.offer(cur.right.val + pathVal * 10);
                }
            }
            return sum;
        }
    }
    
  • 相关阅读:
    js判断是否是ie浏览器且给出ie版本
    自定义的好看的单选复选框功能
    Gaze Estimation学习笔记(2)-It's Written All Over Your Face Full-Face Appearance-Based Gaze Estimation
    Gaze Estimation学习笔记(1)-Appearance-Based Gaze Estimation in the Wild
    软件工程个人总结博客
    软件工程结对编程博客
    软件工程第一次阅读作业
    软工第0次个人作业
    OO第四次博客作业
    OO第三次博客作业
  • 原文地址:https://www.cnblogs.com/PythonFCG/p/13895105.html
Copyright © 2011-2022 走看看