Leetcode dfs Sum Root to Leaf Numbers

Sum Root to Leaf Numbers

Total Accepted: 20237 Total
Submissions: 67979My Submissions

Given a binary tree containing digits from 0-9 only,
each root-to-leaf path could represent a number.

An example is the root-to-leaf path 1->2->3 which
represents the number 123.

Find the total sum of all root-to-leaf numbers.

For example,

    1
   /   2   3

The root-to-leaf path 1->2 represents
the number 12.

The root-to-leaf path 1->3 represents the number 13.

Return the sum = 12 + 13 = 25.

题意:给定一棵二叉树,每个节目包含数字0-9,根到叶子节点组成一个数字,

求所有这些数字的总和

思路:dfs

int sum

void dfs(TreeNode *root, int num)

num表示从根节点到当前节点前面的节点组成数字

这个函数求出,在num的基础上,从root 为根到它的所有叶子节点组成的数字的总和

复杂度:时间 O(n) ;空间 O(log n)

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:

    int sumNumbers(TreeNode *root){
    	return dfs(root, 0);
    }
private:
    int dfs(TreeNode *root, int num){
    	if(!root) {return 0;} //空
    	if(!root->left && !root->right) {return num * 10 + root->val;} //叶子节点
    	return dfs(root->left, num * 10 + root->val) + dfs(root->right, num * 10 + root->val);
    }

};
时间: 2024-10-27 13:34:42

Leetcode dfs Sum Root to Leaf Numbers的相关文章

LeetCode OJ - Sum Root to Leaf Numbers

这道题也很简单,只要把二叉树按照宽度优先的策略遍历一遍,就可以解决问题,采用递归方法越是简单. 下面是AC代码: 1 /** 2 * Sum Root to Leaf Numbers 3 * 采用递归的方法,宽度遍历 4 */ 5 int result=0; 6 public int sumNumbers(TreeNode root){ 7 8 bFSearch(root,0); 9 return result; 10 } 11 private void bFSearch(TreeNode ro

[C++]LeetCode: 94 Sum Root to Leaf Numbers (先序遍历)

题目: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2

[Leetcode][JAVA] Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3  

Java for LeetCode 129 Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

【LeetCode】 Sum Root to Leaf Numbers

第一次写的解法,.对于只有一个儿子的节点重复了两次 ..结果就弄复杂了..我也觉得不应该能有这么多的重复嘛 <span style="font-size:18px;">/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ package java

LeetCode[Tree]: Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / \ 2 3

[LeetCode] 129 Sum Root to Leaf Numbers 求根到叶节点数字之和

此题题意是求所有从根结点到叶节点的路径转化为数字后之和. 因为是二叉树,容易想到是用递归求解. 整体思想是从根到叶子进行遍历,其中不断保存当前的中间结果(上一层的结果乘以10后加上当前层根节点的数值)并通过参数向下传递... 到达叶子节点时可以逐层返回最终结果.解法不难,但有几个细节需要考虑清楚. 1)可以用递归函数dfs的参数表内置的返回和来返回数值,也可以直接用dfs返回值,对应于以下两种定义: void dfs(TreeNode *root, int &sum, int pre); int

【leetcode】Sum Root to Leaf Numbers(hard)

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

【LeetCode】Sum Root to Leaf Numbers 解题报告

[题目] Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 /