程序员的资源宝库

网站首页 > gitee 正文

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

sanyeah 2024-03-29 17:58:04 gitee 8 ℃ 0 评论

题目链接

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

题目描述

解题思路

1.先序遍历(DFS)

2.层序遍历(DFS)

AC代码

DFS解法一

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    List<String> ans = new LinkedList<>();
    void preOrderTraversal(TreeNode root,StringBuffer sb1){
        if(root==null) return;
        if(root.left == null && root.right == null){
            //必须新建两个局部变量,这是局部变量1。这样在递归弹出函数的时候,能够回到当初stringbuffer变量的状态,因为StringBuffer属于局部变量,是每个函数栈特有的。
            StringBuffer sb2 = new StringBuffer(sb1);
            sb2.append(root.val);
            String temp = sb2.toString();
            ans.add(temp);
            return;
        }
        //必须新建两个局部变量,这是局部变量2。
        StringBuffer sb = new StringBuffer(sb1);
        sb.append(root.val);
        preOrderTraversal(root.left,sb);
        preOrderTraversal(root.right,sb);      
    }

    public int stringToint(String s){
        int sum = 0;
        for(int i = s.length()-1; i >= 0; i--){
            sum += (s.charAt(i)-'0')*Math.pow(10,s.length()-1-i);
        }
        return sum;
    }

    public int sumNumbers(TreeNode root) {
        StringBuffer sb1 = new StringBuffer();
        preOrderTraversal(root,sb1);
        int tot = 0;
        for(int i = 0; i < ans.size(); i++){
            tot += stringToint(ans.get(i));
        }
        
        return tot;
    }
}
//将字符串转为数字,可以不调用Math.pow()函数
int res = 0;
for(int i = 0; i < s.length(); i++){
    res = res * 10 + (s.charAt(i)-'0');
}

DFS解法二

这是官方解法,真的非常巧妙!!!!直接在递归的过程中就完成了计算。
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {

    public int dfs(TreeNode root, int sum){
        if(root == null) return 0;
        sum = sum*10+root.val;
        if(root.left==null&&root.right==null) return sum;
        int leftValue = dfs(root.left,sum);
        int rightValue = dfs(root.right,sum);
        return leftValue+rightValue;
    }

    public int sumNumbers(TreeNode root) {
        return dfs(root,0);
    }
}

DFS解法三

class Solution {
    int ans = 0;

    public int sumNumbers(TreeNode root) {
        if (root == null) return 0;
        int temp = 0;
        dfs(root, temp);
        return ans;
    }

    public void dfs(TreeNode root, int temp) {
        temp = temp * 10 + root.val;
        if (root.left == null && root.right == null) {
            ans += temp;
            return;
        }
        if (root.left != null) {
            dfs(root.left, temp);
        }
        if (root.right != null) {
            dfs(root.right, temp);
        }
    }
}

作者:byakuya-2
链接:https://leetcode-cn.com/problems/sum-root-to-leaf-numbers/solution/dfsyong-shi-ji-bai-100nei-cun-ji-bai-95-by-byakuya/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

BFS解法

class Solution {
    public int sumNumbers(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int sum = 0;
        Queue<TreeNode> nodeQueue = new LinkedList<TreeNode>();
        Queue<Integer> numQueue = new LinkedList<Integer>();
        nodeQueue.offer(root);
        numQueue.offer(root.val);
        while (!nodeQueue.isEmpty()) {
            TreeNode node = nodeQueue.poll();
            int num = numQueue.poll();
            TreeNode left = node.left, right = node.right;
            if (left == null && right == null) {
                sum += num;
            } else {
                if (left != null) {
                    nodeQueue.offer(left);
                    numQueue.offer(num * 10 + left.val);
                }
                if (right != null) {
                    nodeQueue.offer(right);
                    numQueue.offer(num * 10 + right.val);
                }
            }
        }
        return sum;
    }
}

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/sum-root-to-leaf-numbers/solution/qiu-gen-dao-xie-zi-jie-dian-shu-zi-zhi-he-by-leetc/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表