二叉树路径求和
来源:互联网 发布:我的同桌是极品网络剧 编辑:程序博客网 时间:2024/05/16 12:00
二叉树路径和
给定一棵二叉树,二叉树权值为0-9,得出所有根节点到叶节点的路径和。如下图的二叉树,路径和n=137+15=152
1
/\
3 5
\
7
知道用递归遍历i每条路径:但是具体做法还是请教了我对象,下面是我的代码,还有个句话要说就是这道题和《剑指offer》中的找到路径和为22的路径相似,都是要找出所有路径,这道题见http://blog.csdn.net/luxiaoxun/article/details/7537605
下面是求每条路径的路径和:
package binayTree;/** * Created by Administrator on 2015/10/30 0030. */import java.util.ArrayList;import java.util.Stack;/** * Created by chen on 2015/10/30. */class Solution1 { ArrayList<String> list1 = new ArrayList<String>(); Integer sum = 0; Stack<Integer> stack=new Stack<Integer>(); public static void main(String[] args) { Solution1 s = new Solution1(); Stack<Integer> stack=new Stack<Integer>(); Node root = new Node(1); Node node1 = new Node(3); Node node2 = new Node(5); Node node3 = new Node(7); root.left = node1; root.right = node2; node1.right = node3; System.out.println(s.sumNumbers(root)); } public int sumNumbers(Node root) { if (root == null) return 0; stack.push(root.value); // root to this leaf load sum if (root.left == null && root.right == null) { int now = 0; for (Integer i : stack) { now = now * 10 + i; } sum += now; } else { if (root.left != null) { sumNumbers(root.left); stack.pop(); } if (root.right != null) { sumNumbers(root.right); stack.pop(); } } return sum; }}
0 0
- 二叉树路径求和
- LintCode 二叉树路径求和
- 【二叉树】二叉树路径求和
- 【回溯】二叉树求和的所有路径
- leetCode 112 二叉树路径求和问题
- 剑指offer之二叉树路径求和
- LintCode-剑指Offer-(376)二叉树路径求和
- 二叉树面试题——节点均为数字的二叉树路径求和
- 重建二叉树遍历路径求和的最小值和最小值的路径的叶子节点
- 【经典数据结构算法】(4)二叉树求和为定值的所有路径
- C语言强化(四)求和为某个值的二叉树路径
- leetcode_112. Path Sum 二叉树路径求和,判断是否存在路径和为某个值的路径,深度遍历,递归的return
- Path Sum II 二叉树求和
- 二叉树路径和
- 二叉树最长路径
- 二叉树路径和
- 二叉树路径问题
- 二叉树--最小路径
- 快乐数
- NSURLSession的介绍与基本用法
- 【csdn 第一篇】Android分区 mount 权限命令
- Jackson 2开发详解
- 出门问问面试题2016
- 二叉树路径求和
- 使用Genymotion调试出现错误INSTALL_FAILED_CPU_ABI_INCOMPATI
- 三目运算符"?:"的透彻分析
- UIPressPhase API
- 工作日志(2015.10.30)
- android---(async-http)
- 详解VMware Workstation 12 安装虚拟机
- Java实现简易的文本编辑器
- 黑马程序员——反射其他