minimum subtree
来源:互联网 发布:音乐间谍类似软件 编辑:程序博客网 时间:2024/05/16 09:04
Given a binary tree, find the subtree with minimum sum. Return the root of the subtree.
/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */public class Solution { /** * @param root the root of binary tree * @return the root of the minimum subtree */ private int sum = Integer.MAX_VALUE; private TreeNode node = null; public TreeNode findSubtree(TreeNode root) { // Write your code here if (root == null) { return node; } findSum(root); return node; } public int findSum(TreeNode root) { if (root == null) { return 0; } int left = findSum(root.left); int right = findSum(root.right); int summary = root.val + left + right; if (summary < sum) { sum = summary; node = root; } return summary; }}
阅读全文
0 0
- Minimum Subtree
- Minimum Subtree
- minimum subtree
- LintCode Minimum Subtree java solution
- Subtree
- Subtree
- subTree
- Educational Codeforces Round 33 F. Subtree Minimum Query
- codeforces 893F Subtree Minimum Query 线段树合并
- [Codeforces 893F. Subtree Minimum Query]线段树合并
- Educational Codeforces Round 33 (Rated for Div. 2) F. Subtree Minimum Query (线段树)
- #245 Subtree
- Minimum
- max sum of subtree
- max sum of subtree
- Find minimal subtree
- 代码面试题:Subtree
- Subtree(java)
- 华为迷宫问题 广度优先遍历 BFS
- 使用 eclipse 和 插件 开发 Kotlin
- Github的入门常识
- java中File类的getPath(),getAbsolutePath(),getCanonicalPath()区别
- js设计模式--链式调用
- minimum subtree
- 【ERROR 1366 (HY000): Incorrect string value】(关键词:字符集/character set)
- <c语言经典100例>c3 完全平方数
- linux使用ultraedit的FTP的功能出现的不能初始化的问题
- junit 4.12 学习笔记
- Git上传项目至远程仓库与同步到本地
- NYOJ 599 奋斗的小蜗牛
- 文章标题
- 注册事件响应函数