题目 将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树。 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1。 示例: 给定有序数组: [-10,-3,0,5,9], 一个可能的答案是:[0,-3,9,-10,null,5],它可以表示下面这个高度平 ...
分类:
编程语言 时间:
2020-09-16 12:21:04
阅读次数:
31
解析:https://leetcode-cn.com/problems/shu-de-zi-jie-gou-lcof/solution/mian-shi-ti-26-shu-de-zi-jie-gou-xian-xu-bian-li-p/ bool isSubStructure(TreeNode* ...
分类:
其他好文 时间:
2020-09-16 12:19:57
阅读次数:
34
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), r ...
分类:
其他好文 时间:
2020-09-16 12:19:36
阅读次数:
29
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of th ...
分类:
其他好文 时间:
2020-09-15 21:24:11
阅读次数:
42
//前序遍历 /** * 根-左-右,所以入栈的时候要相反,有右节点则加入右节点,有左节点则加入左节点,每车循环的时候,弹一个 */ public ArrayList<Integer> preorderTraversal(TreeNode root) { ArrayList<Integer> lis ...
分类:
编程语言 时间:
2020-09-11 14:23:16
阅读次数:
41
class Solution(object): # 递归思路: # (1)如果二叉树为空,节点个数为0 # (2)如果二叉树不为空,二叉树节点个数 = 左子树节点个数 + 右子树节点个数 + 1 def countNodes(self, root): """ :type root: TreeNode ...
分类:
其他好文 时间:
2020-09-11 14:10:52
阅读次数:
39
输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。 Python # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.r ...
分类:
其他好文 时间:
2020-09-02 16:53:04
阅读次数:
42
二叉树节点函数定义: /** * Definition for a binary tree node. */ function TreeNode(val){ this.val = val; this.left = this.right = null; } 层次遍历构建二叉树(广度优先) functi ...
分类:
其他好文 时间:
2020-08-26 18:35:16
阅读次数:
74
int height(struct TreeNode* root) { if (root == NULL) { return 0; } else { return fmax(height(root->left), height(root->right)) + 1; } } bool isBalanc ...
分类:
其他好文 时间:
2020-08-19 19:58:57
阅读次数:
65
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] 1 \ 2 / 3 Output: [1,3,2] Follow up: Recursive so ...
分类:
其他好文 时间:
2020-08-17 17:50:25
阅读次数:
81