随笔分类 -  二叉树

摘要:题目: 剑指offer的题目有挺多都挺典型的,就像这一道。不过书中的代码写的真是ugly,有很多题目LeetCode上都有,可以去LeetCode讨论区看看,经常有一些大神分享,写的代码真是高效、简洁、清晰,剑指offer上的代码不仅变量名定义又长又不知所云,让人看着就很不清晰明了,还有各种没必要的 阅读全文
posted @ 2018-09-16 00:08 丶Blank 阅读(454) 评论(0) 推荐(0)
摘要:Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to thedefinition of LCA on Wikipedia: “The lowest ... 阅读全文
posted @ 2015-09-20 16:39 丶Blank 阅读(206) 评论(0) 推荐(0)
摘要:Given acompletebinary tree, count the number of nodes.Definition of a complete binary tree fromWikipedia:In a complete binary tree every level, except... 阅读全文
posted @ 2015-06-25 12:33 丶Blank 阅读(175) 评论(0) 推荐(0)
摘要:Invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1Trivia:This problem was inspired bythis... 阅读全文
posted @ 2015-06-25 11:11 丶Blank 阅读(154) 评论(0) 推荐(0)
摘要:Given a binary tree, return thepreordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[1,2,3].Not... 阅读全文
posted @ 2015-05-24 20:28 丶Blank 阅读(98) 评论(0) 推荐(0)
摘要:Givenn, how many structurally uniqueBST's(binary search trees) that store values 1...n?For example,Givenn= 3, there are a total of 5 unique BST's. 1... 阅读全文
posted @ 2015-05-01 14:06 丶Blank 阅读(166) 评论(0) 推荐(0)
摘要:Given a binary tree, return theinordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[1,3,2].Note... 阅读全文
posted @ 2015-04-23 21:40 丶Blank 阅读(166) 评论(0) 推荐(0)
摘要:Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only n... 阅读全文
posted @ 2015-04-22 22:40 丶Blank 阅读(293) 评论(0) 推荐(0)
摘要:Given a binary tree, return thezigzag level ordertraversal of its nodes' values. (ie, from left to right, then right to left for the next level and al... 阅读全文
posted @ 2015-04-21 21:47 丶Blank 阅读(159) 评论(0) 推荐(0)
摘要:Given preorder and inorder traversal of a tree, construct the binary tree.题目大意:给定一个二叉树的前序和中序序列,构建出这个二叉树。解题思路:跟中序和后序一样,在先序中取出根节点,然后在中序中找到根节点,划分左右子树,递归构... 阅读全文
posted @ 2015-04-21 15:42 丶Blank 阅读(153) 评论(0) 推荐(0)
摘要:Given inorder and postorder traversal of a tree, construct the binary tree.题目大意:给定一个二叉树的中序和后续序列,构建出这个二叉树。解题思路:首先后序序列的最后一个是根节点,然后在中序序列中找到这个节点,中序序列中这个节点... 阅读全文
posted @ 2015-04-21 15:26 丶Blank 阅读(165) 评论(0) 推荐(0)
摘要:Given an array where elements are sorted in ascending order, convert it to a height balanced BST.题目大意:给定一个升序序列的数组,将其转换为二叉搜索树。解题思路:数组中间元素是根元素,根元素将数组划分为... 阅读全文
posted @ 2015-04-20 21:28 丶Blank 阅读(140) 评论(0) 推荐(0)
摘要:Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened t... 阅读全文
posted @ 2015-04-13 20:36 丶Blank 阅读(116) 评论(0) 推荐(0)
摘要:Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.Callingnext()will return the next... 阅读全文
posted @ 2015-04-13 19:53 丶Blank 阅读(140) 评论(0) 推荐(0)
摘要:Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number.An example is the root-to-leaf path1->2->3which rep... 阅读全文
posted @ 2015-04-05 23:17 丶Blank 阅读(115) 评论(0) 推荐(0)
摘要:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1 / \ ... 阅读全文
posted @ 2015-03-22 18:07 丶Blank 阅读(166) 评论(0) 推荐(0)
摘要:Given a binary tree, return thebottom-up level ordertraversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For exa... 阅读全文
posted @ 2015-03-22 14:01 丶Blank 阅读(167) 评论(0) 推荐(0)
摘要:Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth... 阅读全文
posted @ 2015-03-22 12:07 丶Blank 阅读(169) 评论(0) 推荐(0)
摘要:Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest le... 阅读全文
posted @ 2015-03-22 00:18 丶Blank 阅读(202) 评论(0) 推荐(0)
摘要:题目描述:二叉树的前序、中序、后序遍历的定义:前序遍历:对任一子树,先访问跟,然后遍历其左子树,最后遍历其右子树;中序遍历:对任一子树,先遍历其左子树,然后访问根,最后遍历其右子树;后序遍历:对任一子树,先遍历其左子树,然后遍历其右子树,最后访问根。给定一棵二叉树的前序遍历和中序遍历,求其后序遍历(提示:给定前序遍历与中序遍历能够唯一确定后序遍历)。输入:两个字符串,其长度n均小于等于26。第一行为前序遍历,第二行为中序遍历。二叉树中的结点名称以大写字母表示:A,B,C....最多26个结点。输出:输入样例可能有多组,对于每组测试样例,输出一行,为后序遍历的字符串。样例输入:ABCBACFDX 阅读全文
posted @ 2013-03-03 17:45 丶Blank 阅读(243) 评论(0) 推荐(0)