LeetCode Sum Root to Leaf Numbers(DFS)
题意:
给一棵二叉树,每个节点上有一个数字,范围是0~9,将从根到叶子的所有数字作为一个串,求所有串的和。
思路:
普通常规的DFS。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int sumNumbers(TreeNode* root) {
if(root==NULL) return ;
return DFS(root,);
}
int DFS(TreeNode* t,int sum)
{
sum=sum*+t->val;
if(!t->left && !t->right) return sum;
int left=, right=;
if(t->left) left=DFS(t->left,sum);
if(t->right) right=DFS(t->right,sum);
return left+right;
}
};
AC代码
LeetCode Sum Root to Leaf Numbers(DFS)的更多相关文章
- 【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 ...
- LeetCode: Sum Root to Leaf Numbers 解题报告
Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf path ...
- [Leetcode] Sum root to leaf numbers求根到叶节点的数字之和
Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number. ...
- LeetCode :: Sum Root to Leaf Numbers [tree、dfs]
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- LeetCode OJ:Sum Root to Leaf Numbers(根到叶节点数字之和)
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- [LeetCode] Sum Root to Leaf Numbers dfs,深度搜索
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- [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 ...
- 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 ...
- LeetCode: Sum Root to Leaf Numbers [129]
[题目] Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a n ...
随机推荐
- long型转日期型
//时分秒格式//不知为何,出来的时间有点差别 public class Test { public static void main(String[] args) throws Exception ...
- C#图书资源【更新中...】喜欢的就转存吧
百度分享暂时取消了,需要的朋友可以直接关注我,我发给你. 重点篇 1.C#与.NET3.5高级程序设计 2.C#与.NET3.5高级程序设计.rar 3.Effective_C#_中文版_改善C#程序 ...
- Windows API 文件处理
CloseHandle 关闭一个内核对象.其中包括文件.文件映射.进程.线程.安全和同步对象等 CompareFileTime 对比两个文件的时间 CopyFile 复制文件 CreateDirect ...
- Learn clojure in Y minutes
Learn X in Y minutes Where X=clojure Get the code: learnclojure.clj Clojure is a Lisp family languag ...
- Linux 常用命令杂记
移动光标:h:向左移动j:向下移动k:向上移动l:向上移动 与window 光标移动键功能一致. 常用命令行:shift + ^ 行首shift + $ 行尾ctrl + v 可视模式 , 选择一个范 ...
- JDBC 连接池
数据库连接池(connection pool) JDBC数据库连接池的必要性 在使用开发基于数据库的web程序时,传统的模式基本是按以下步骤: 在主程序(如servlet.beans)中建立数据库连接 ...
- Rhel6-mailsystem配置文档
(postfix+dovecot+mysql+extmail) 理论基础:
- java.io.IOException: open failed: EACCES (Permission denied)问题解决
1. 问题描述:在Android中,用程序访问Sdcard时,有时出现“java.io.IOException: open failed: EACCES (Permission denied)&qu ...
- 安装eclipse for c/c++环境
安装eclipse for c/c++环境: 1.启动eclipse, 2.选择Help->Install New Software...,在Work with的框框下复 ...
- iOS开发之App启动原理
iOS程序的启动过程 程序启动的完整过程大致步骤如下: 1.main函数 2.UIApplicationMain * 创建UIApplication对象 * 创建UIApplication的deleg ...