二叉树
bh_xiaoxinba
这个作者很懒,什么都没留下…
展开
-
二叉树系列---找到累加和为指定值的最长路径长度
题目给定一颗二叉树和一个sum,二叉树节点值为整数,求累加和为sum的最大路径长度; 路径是指从某个节点往下,每次最多选择一个孩子节点或者不选择所形成的节点链;思路已知头结点head和给定值sum;求maxLen(maxLen保存最大路径长度,整个过程中,需要比较maxLen,所以设定maxLen为变量);对于当前节点,求出头到该节点的累加和,并存入sumMap中; 判断在以该节点为结尾的情况下,原创 2016-11-04 01:31:25 · 1762 阅读 · 2 评论 -
二叉树系列---求所有从根到叶子路径组成的数的和
题目:给定一个二叉树,从根到叶子的所经过的所有节点的值组成一个数,求所有路径数的和; 比如, 1 / \ 2 312+13=25实现:从上向下求,递归; 根:sum=val; 向下:sum=sum*10+val 如果是叶子,则返回; 非叶子,则左的和+右的和;代码: public int sumNumbers(Node node){ int原创 2016-11-02 20:06:52 · 1204 阅读 · 0 评论 -
二叉树系列---求出二叉树的最小深度
求出二叉树的最小深度;分析: 空树:1 只有根:1 只有左子树或只有右子树:子树深度+1; 既有左,又有右子树:min(左右子树深度)+1;实现:public int minDepth(Node root){ if(root==null){ return 0; } int ldp=minDepth(root.left);原创 2016-11-02 17:57:54 · 247 阅读 · 0 评论 -
二叉树系列---构造二叉查找树
构造二叉查找树package binary_tree;public class BuildBinarySearchTree { private Node root; public BuildBinarySearchTree(){ root=null; } public void insert(int data){ Node newNode=n原创 2016-11-02 12:19:43 · 343 阅读 · 0 评论 -
二叉树系列---求二叉树中两个节点之间的最大距离
题目求二叉树中两个节点之间的最大距离(节点之间的距离:这两个节点之间边的个数); 计算一个二叉树的最大距离有两个情况:情况A: 路径经过左子树的最深节点,通过根节点,再到右子树的最深节点。 情况B: 路径不穿过根节点,而是左子树或右子树的最大距离路径,取其大者。 只需要计算这两个情况的路径距离,并取其大者,就是该二叉树的最大距离。 方法1求左子树到根的最大距离leftMaxDistance;原创 2016-11-02 16:46:45 · 1308 阅读 · 0 评论 -
二叉树序列---根据后序和中序系列或前序和中序序列构造二叉树
1、根据前序遍历序列和中序遍历序列,构造二叉树(或求后序遍历序列);public Node buildTree(int[] preOrder,int[] inOrder){ Node root=buildTree(preOrder,0,preOrder.length-1,inOrder,0,inOrder.length-1); return root; }原创 2016-11-02 15:13:44 · 616 阅读 · 0 评论 -
二叉树系列---基础
1、递归定义 二叉树或者是一颗空树,或者是以颗由一个根节点和两颗互不相交的分别成为根节点的左子树和右子树组成的非空树,左右子树也是一颗二叉树; 2、基本概念 叶节点; 分支节点; 深度; 度;完全二叉树; 满二叉树; 二叉排序树(二叉查找树)3、性质 非空二叉树的第i层最多的节点数; 深度为k的二叉树,最多包含的节点数,最少的节点数; 度为0的节点数n0,度为1的节点数n1,度原创 2016-11-02 11:31:57 · 276 阅读 · 0 评论 -
二叉树系列---symmetric_tree
题目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 / \ 2 2 / \ / \ 3 4 4 3But the follow原创 2016-11-05 22:25:56 · 292 阅读 · 0 评论 -
二叉树系列---层次遍历二叉树
层次遍历借助队列public void layerTranverse(Node root){ if(root==null){ return; } Queue<Node> queue=new LinkedList<Node>(); queue.add(root); while(!queue.isEmp原创 2016-11-02 14:03:08 · 290 阅读 · 0 评论 -
二叉树系列---same-tree
题目 给定两颗二叉树,判断这两颗二叉树是否相同; Two binary trees are considered equal if they are structurally identical and the nodes have the same value.实现 递归:判断根节点的值;判断左子树和右子树;public boolean isSameTree(TreeNode p, Tree原创 2016-11-05 17:59:48 · 257 阅读 · 0 评论 -
二叉树系列---recover_binary_search_tree
题目在一个二叉查找树中,有两个节点被错误的交换了,实现一个函数得出正确的二叉查找树;思路利用二叉查找树性质:中序遍历有序; *先得到中序序列; * 正向变量中序序列,遇到逆序的就break; * 反向遍历中序序列,遇到逆序的就break; * 交换两个break处的元素即可;实现public void recoverTree(TreeNode root){原创 2016-11-05 17:44:51 · 298 阅读 · 0 评论 -
二叉树系列---判断给定的二叉树是否为二叉查找树
题目判断给定的二叉树是否为二叉查找树方法1递归判断:先判断左子树是否为二叉查找树;再判断右子树;最后判断根(根小于左子树的最大节点值,大于右子树的最左节点值);public boolean isValidBST(TreeNode root){ if(root==null){ return true; } if(root.left原创 2016-11-05 16:40:24 · 805 阅读 · 0 评论 -
二叉树系列---求包含n个节点的二叉查找树的种类数
题目求包含n个节点的二叉树的种类数;方法1设dp[i]表示共有i个节点时,能产生的BST树的个数 n == 0 时,空树的个数必然为1,因此dp[0] = 1 n == 1 时,只有1这个根节点,数量也为1,因此dp[1] = 1 当根节点元素为 1, 2, 3, 4, 5, …, i, …, n时,基于以下原则的BST树具有唯一性:原创 2016-11-05 15:19:26 · 5667 阅读 · 1 评论 -
二叉树系列---判断二叉树是否为平衡二叉树
题目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 of the two subtrees of every node never differ原创 2016-11-04 04:53:44 · 241 阅读 · 0 评论 -
二叉树系列---path-sum-II
题目Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum. For example: Given the below binary tree andsum = 22, return [ [5,4,11,原创 2016-11-04 04:36:27 · 378 阅读 · 0 评论 -
二叉树系列---path-sum
题目Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree andsum原创 2016-11-04 04:21:26 · 348 阅读 · 0 评论 -
二叉树系列---标记二叉树中每个节点的next指针
题目Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node. If t原创 2016-11-04 04:07:02 · 1867 阅读 · 0 评论 -
二叉树系列---在二叉树中找到两个节点的最近公共祖先
题目给定一颗二叉树的头结点,和这颗二叉树中2个节点n1和n2,求这两个节点的最近公共祖先;思路利用后序遍历实现; 对于当前节点cur,如果节点为null或者等于n1或n2中的一个,则直接返回cur; 先处理左右子树,左子树返回left,右子树返回right;判断left和right; 1)left和right均为null,说明以cur为根的树上没发现n1和n2; 2)left和right均不原创 2016-11-04 02:11:59 · 2748 阅读 · 0 评论 -
二叉树系列---求二叉树的最大路径和
题目Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / \ 2 3Return6.思路/*对于给定节点,以该节原创 2016-11-02 21:23:21 · 684 阅读 · 0 评论