
Tree
Crystal_ting
个人博客 limengting.site
展开
-
513. Find Bottom Left Tree Value(Java)
Given a binary tree, find the leftmost value in the last row of the tree.Example 1: Input: 2 / \ 1 3Output: 1Example 2: Input: 1 / \ 2 3 / / \ 4 5 6原创 2017-08-14 16:21:24 · 319 阅读 · 0 评论 -
《剑指offer》对称的二叉树
题目描述 请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。 /* public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { ...原创 2018-04-30 17:40:42 · 197 阅读 · 0 评论 -
《剑指offer》树的子结构
题目描述 输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)/** public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val原创 2018-05-02 13:57:35 · 244 阅读 · 0 评论 -
《剑指offer》二叉搜索树的后序遍历序列
题目描述 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。 public class Solution { public boolean VerifySquenceOfBST(int [] sequence) { if (sequence == null || sequence...原创 2018-05-02 14:42:38 · 245 阅读 · 0 评论 -
[4]《剑指offer》二叉树中和为某一值的路径
题目描述 输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。 import java.util.ArrayList; /** public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = ...原创 2018-05-02 15:28:57 · 230 阅读 · 0 评论 -
[4]《剑指offer》二叉搜索树与双向链表
题目描述 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。 方法一:非递归版 解题思路: 1.核心是中序遍历的非递归算法。 2.修改当前遍历节点与前一遍历节点的指针指向。 /** public class TreeNode { int val = 0; TreeNode left = null; ...原创 2018-05-04 20:20:53 · 274 阅读 · 0 评论 -
[4]《剑指offer》二叉搜索树的第k个节点
题目描述 给定一颗二叉搜索树,请找出其中的第k大的结点。例如, 5 / \ 3 7 /\ /\ 2 4 6 8 中,按结点数值大小顺序第三个结点的值为4。 /* public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(...原创 2018-05-04 22:48:59 · 245 阅读 · 0 评论 -
《剑指offer》平衡二叉树
题目描述 输入一棵二叉树,判断该二叉树是否是平衡二叉树。 public class Solution { public static class ReturnData { public boolean isBalanced; public int height; public ReturnData(boolean isBalanced,...原创 2018-04-27 22:17:45 · 218 阅读 · 0 评论 -
《剑指offer》二叉树的深度
题目描述 输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。 方法一:递归,时间复杂度O(logn) /** public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public...原创 2018-04-27 23:12:37 · 221 阅读 · 0 评论 -
《剑指offer》二叉树的镜像
题目描述 操作给定的二叉树,将其变换为源二叉树的镜像。 输入描述: 二叉树的镜像定义: 源二叉树: 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树: 8 / \ 10 6 ...原创 2018-04-30 13:38:06 · 207 阅读 · 0 评论 -
面试常见算法1:排序算法
(1)冒泡排序: 算法描述:从头开始,两两交换,大的沉底,每轮确定一个最大的数,每轮逐渐缩小范围end- -,直到end = 1,即只有两个数 时间复杂度:O(n²) 空间复杂度:O(1) 稳定性:可以做到稳定的,遇到相等的后面的代替前面的沉底 package sword_to_offer_sort; import java.util.Scanner; public class ...原创 2018-04-10 19:23:51 · 761 阅读 · 0 评论 -
面试常见算法2:对树的操作
1、树的遍历:先序/中序/后序/层次遍历 package sword_to_offer_tree; import java.util.LinkedList; import java.util.Queue; import java.util.Stack; public class PreInPosLayerTraversal { public static class Node {...原创 2018-04-15 15:56:46 · 411 阅读 · 0 评论 -
515. Find Largest Value in Each Tree Row(Java)
You need to find the largest value in each row of a binary tree.Example:Input: 1 / \ 3 2 / \ \ 5 3 9 Output: [1, 3, 9]/** * Definition for a binary tr原创 2017-08-14 17:29:47 · 293 阅读 · 0 评论 -
617. Merge Two Binary Trees(Java)
Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.You need to merge them into a new binary tree. T原创 2017-08-14 10:44:24 · 441 阅读 · 0 评论 -
100. Same Tree(Java)
Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value./** * Definition f原创 2017-08-14 10:23:12 · 250 阅读 · 0 评论 -
230. Kth Smallest Element in a BST(Java)
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note: You may assume k is always valid, 1 ? k ? BST’s total elements.Follow up: What if the BST is mod原创 2017-08-14 22:06:27 · 359 阅读 · 0 评论 -
108. Convert Sorted Array to Binary Search Tree(Java)
Given an array where elements are sorted in ascending order, convert it to a height balanced BST./** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode lef原创 2017-08-14 22:17:32 · 297 阅读 · 0 评论 -
?《剑指offer》7.重建二叉树
题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。 思路:先序的第一个是根节点,根据根节点在中序中的位置将中序划分成左右子树,再递归实现,直到只剩一个结点再递归就会使得其左右子节点都为null并逐层返...原创 2018-03-25 09:50:26 · 180 阅读 · 0 评论 -
《剑指offer》8.二叉树的下一个节点
题目描述 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。 思路:如果有右子树,则找右子树的最左节点;如果没有右子树,则向上递归找该节点属于哪个父节点左支,则返回该父节点 /* public class TreeLinkNode { int val; TreeLinkNode left = ...原创 2018-03-25 10:19:08 · 176 阅读 · 0 评论 -
《剑指offer》从上到下打印二叉树
题目描述 从上往下打印出二叉树的每个节点,同层节点从左至右打印。 /** public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } }...原创 2018-04-29 16:37:37 · 210 阅读 · 0 评论