递归实现结构很好记,上来写两递归,递归左子树,递归右子树。 前序遍历,访问节点(打印节点)在两个递归前面——中、左、右; 中序遍历,访问放递归中间——左中右; 后序遍历,先两递归,最后才访问——左、中、右。 1)先序遍历void preorder(BiTree T){ if (T != NULL) ...
分类:
其他好文 时间:
2020-05-14 15:51:55
阅读次数:
66
思路:前序是根左右,前序序列第一个元素一定是根。中序是左,根,右。根节点左边一定是左子树,右边一定是右子树。 树没有重复元素,所以,先找出根节点,初始化一个TreeNode root,再根据数值相同,找中序遍历里面的根节点,之后用Arrays.copyOfRange(preorder,1,num+1 ...
分类:
其他好文 时间:
2020-05-06 12:18:34
阅读次数:
53
题目: 解法: 方法一:先序遍历 1 class Solution { 2 public boolean isSubtree(TreeNode s, TreeNode t) { 3 String tree1 = preOrder(s, true); 4 String tree2 = preOrder ...
分类:
其他好文 时间:
2020-05-03 15:01:16
阅读次数:
50
/* program to construct tree using inorder and preorder traversals */ #include <stdio.h> #include <stdlib.h> /* A binary tree node has data, pointer t ...
分类:
其他好文 时间:
2020-05-03 10:19:45
阅读次数:
64
验证前序遍历序列二叉搜索树。题意是给一个二叉搜索树的前序遍历的结果,请你验证这个结果是否正确。例子, Consider the following binary search tree: 5 / \ 2 6 / \ 1 3 Example 1: Input: [5,2,6,1,3] Output: ...
分类:
其他好文 时间:
2020-05-02 09:53:55
阅读次数:
58
```/** * 144. Binary Tree Preorder Traversal * 1. Time:O(n) Space:O(n) * 2. Time:O(n) Space:O(n) * 3. Time:O(n) Space:O(n) * 4. Time:O(n) Space:O(1) *... ...
分类:
其他好文 时间:
2020-04-27 13:11:07
阅读次数:
39
从前序与中序遍历序列构造二叉树。题意是给一个二叉树的前序遍历和中序遍历,请根据这两个遍历,把树构造出来。例子, For example, given preorder = [3,9,20,15,7] inorder = [9,3,15,20,7] Return the following binar ...
分类:
其他好文 时间:
2020-04-21 13:31:31
阅读次数:
65
先序遍历构造二叉搜索树。题目即是题意,例子, Input: [8,5,1,7,10,12] Output: [8,5,10,1,7,null,12] 这个题可以迭代或递归都可以做,我这里暂时先给出递归的做法。因为是BST所以会简单很多,首先input的首个元素是树的根节点,接着写一个helper函数 ...
分类:
其他好文 时间:
2020-04-21 09:34:03
阅读次数:
63
题目: 输入某二叉树的前序遍历和中序遍历的结果,请重建该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。 例如,给出 前序遍历 preorder = [3,9,20,15,7]中序遍历 inorder = [9,3,15,20,7]返回如下的二叉树: 3 / \ 9 20 / \ 15 ...
分类:
其他好文 时间:
2020-04-17 00:02:57
阅读次数:
73
地址:https://leetcode cn.com/problems/binary tree preorder traversal/submissions/ 大意:前序遍历一棵树 ` ` ...
分类:
其他好文 时间:
2020-04-12 18:34:41
阅读次数:
64