1.题目描述 2.问题分析 使用层序遍历思想 3.代码 int findBottomLeftValue(TreeNode* root) { if (root == NULL) ; queue<TreeNode*> q; q.push(root); ; while (!q.empty()) { int size = q.size(); ; i < size; i++) { TreeNode *node = q.front(); if (node->left != NULL) q.pu…
leetcode算法: Find Bottom Left Tree ValueGiven 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 / 7Output:7Note: You may assume the tree (i.e., the given root n…
题目说明 Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 4 6 The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6 题目分析 第一感觉是前序遍历,顺便打算在这题练习一下昨天学到的二级指针的写法XD,调的时候bug挺多的,可读性贼差,指针还是慎用啊-- 以下为个人实现(C++,12ms):…
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 题解: 题意比较清楚, 找到从root出发最长的一条路径的长度. 采用DFS即可. 相似的一道题: Minimux Depth of Binary Tree 解法: http://…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS Date 题目地址:https://leetcode.com/problems/find-bottom-left-tree-value/#/description 题目描述 Given a binary tree, find the leftmost value in the last row of the tree. Example…
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 leaf node. 题意很清楚,找到二叉树中深度最短的一条路径,DFS(貌似是面试宝典上的一道题) 类似的一道题:http://www.cnblogs.com/double-win/p/3737262…
原题 Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input: 2 / 1 3 Output: 1 Example 2: Input: 1 / \ 2 3 / / \ 4 5 6 / 7 Output: 7 Note: You may assume the tree (i.e., the given root node) is not NULL. 解析 查找最后一层最左侧…
1.题目描述 2.问题分析 判断一个节点,然后判断子树. 3.代码 bool isSubtree(TreeNode* s, TreeNode* t) { if (s == NULL) return false; else { return isSame(s,t) || isSubtree(s->left, t) || isSubtree(s->right, t); } } bool isSame(TreeNode *t1, TreeNode *t2) { if (t1 == NULL &…
1.题目描述 2.分析 深度优先. 3.代码 int ans; int diameterOfBinaryTree(TreeNode* root) { ans = ; depth(root); ; } int depth(TreeNode *root){ if (root == NULL) ; int L = depth(root->left); int R = depth(root->right); ans = max(ans, L+R+); ; }…
1.题目描述 2/问题分析 利用中序遍历,然后重新构造树. 3.代码 TreeNode* increasingBST(TreeNode* root) { if (root == NULL) return NULL; vector<int> v; inorder(root,v); TreeNode* dummy = ); TreeNode *p = dummy; for (vector<int>::iterator it = v.begin(); it != v.end(); it+…