
二叉树
wutingyehe
Just for fun
展开
-
LintCode 二叉树的最小深度
给定一个二叉树,找出其最小深度。 二叉树的最小深度为根节点到最近叶子节点的距离。样例 给出一棵如下的二叉树: 1 / \ 2 3 / \ 4 5 这个二叉树的最小深度为 2Given a binary tree, find its minimum depth. The minimum depth is the number of n原创 2015-06-26 10:33:21 · 1125 阅读 · 0 评论 -
[LintCode] 二叉树的路径之和 Binary Tree Path Sum
给定一个二叉树,找出所有路径中各节点相加总和等于给定 目标值 的路径。 一个有效的路径,指的是从根节点到叶节点的路径。样例 给定一个二叉树,和 目标值 = 5: 1 / \ 2 4 / \ 2 3 返回: [ [1, 2, 2], [1, 4] ]Given a binary tree, find all paths that s原创 2016-04-22 09:38:38 · 3909 阅读 · 1 评论 -
[LintCode] 二叉树的层序遍历 Binary Tree Level Order Tranversal
给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问)样例 给一棵二叉树 {3,9,20,#,#,15,7} : 3 / \ 9 20 / \ 15 7 返回他的分层遍历结果: [ [3], [9,20], [15,7] ]只使用一个队列:/** * Definition of TreeNode: * public class Tree原创 2016-04-20 21:06:18 · 674 阅读 · 0 评论 -
[LintCode] 中序遍历和后序遍历树构造二叉树 Construct Binary Tree from Inorder and Postorder Traversal
根据中序遍历和后序遍历树构造二叉树 注意事项 你可以假设树中不存在相同数值的节点 样例 给出树的中序遍历: [1,2,3] 和后序遍历: [1,3,2] 返回如下的树: 2 / \ 1 3Given inorder and postorder traversal of a tree, construct the binary tree. Notice You may原创 2016-04-18 20:25:03 · 701 阅读 · 0 评论 -
[LintCode] 前序遍历和中序遍历树构造二叉树 Construct Binary Tree from Preorder and Inorder Traversal
根据前序遍历和中序遍历树构造二叉树. 注意事项 你可以假设树中不存在相同数值的节点.样例 给出中序遍历:[1,2,3]和前序遍历:[2,1,3]. 返回如下的树: 2 / \ 1 3Given preorder and inorder traversal of a tree, construct the binary tree. Notice You may assume原创 2016-04-17 22:27:18 · 578 阅读 · 0 评论 -
[LintCode] 将二叉查找树转换成双链表 Convert Binary Search Tree to Doubly Linked List
将一个二叉查找树按照中序遍历转换成双向链表。 样例 给定一个二叉查找树: 4 / \ 2 5 / \ 1 3 返回 1<->2<->3<->4<->5。Convert a binary search tree to doubly linked list with in-order traversal. Example Given a binary se原创 2016-04-25 22:26:32 · 2423 阅读 · 0 评论 -
LintCode Insert a Node in a Binary Search Tree 在二叉查找树中插入节点
给定一棵二叉查找树和一个新的树节点,将节点插入到树中。 你需要保证该树仍然是一棵二叉查找树。 Given a binary search tree and a new tree node, insert the node into the tree. You should keep the tree still be a valid binary search tree.样例 给出如下一棵二叉原创 2015-07-14 17:19:01 · 1273 阅读 · 0 评论 -
LintCode Binary Tree Postorder Traversal 二叉树的后序遍历
给出一棵二叉树,返回其节点值的后序遍历。 Given a binary tree, return the postorder traversal of its nodes’ values.样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [3,2,1]解法: 使用两个栈,第二个栈记录第一个栈对应节点被访问的次数。只有当被访问原创 2015-07-14 14:45:06 · 614 阅读 · 0 评论 -
LintCode Binary Tree Preorder Traversal二叉树的前序遍历(非递归)
给出一棵二叉树,返回其节点值的前序遍历。 Given a binary tree, return the preorder traversal of its nodes’ values.样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [1,2,3]. 1)将二叉树节点入栈(入栈之前输出该节点值),直到左孩子为null为止原创 2015-07-14 13:49:47 · 674 阅读 · 0 评论 -
LintCode Binary Tree Inorder Traversal 二叉树的中序遍历(非递归)
给出一棵二叉树,返回其中序遍历。 Given a binary tree, return the inorder traversal of its nodes’ values.样例 给出二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [1,3,2]./** * Definition of TreeNode: * public clas原创 2015-07-14 14:03:08 · 744 阅读 · 0 评论 -
LinCode Maximum Depth of Binary Tree 二叉树的最大深度
中文版: 给定一个二叉树,找出其最大深度。 二叉树的深度为根节点到最远叶子节点的距离。样例 给出一棵如下的二叉树: 1 / \ 2 3 / \ 4 5 这个二叉树的最大深度为3.English Version: Given a binary tree, find its maximum depth. The maximum depth is the n原创 2015-07-11 21:40:59 · 506 阅读 · 0 评论 -
【Leetcode】Merge Two Binary Trees 合并两个二叉树
合并两个二叉树原创 2017-10-27 21:26:46 · 545 阅读 · 0 评论