LeeCode 二叉树问题(一)】的更多相关文章

leecode刷题(30)-- 二叉树的后序遍历 二叉树的后序遍历 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 思路 跟上道题一样,我们使用递归的思想解决. 后序遍历: 先处理左子树,然后是右子树,最后是根 代码如下 Java 描述 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode lef…
leecode刷题(29)-- 二叉树的中序遍历 二叉树的中序遍历 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 思路 跟上一道题一样,用递归的思想很快就能解决. 中序遍历: 先处理左子树,然后是根,最后是右子树. 代码如下 Java 描述 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode l…
leecode刷题(28)-- 二叉树的前序遍历 二叉树的前序遍历 给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 思路 这道题我们用递归的思想很容易就能解出来.前序遍历,我们先处理根,之后是左子树,然后是右子树. 代码如下 Java 描述 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode l…
leecode刷题(24)-- 翻转二叉树 翻转二叉树 翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 备注: 这个问题是受到 Max Howell的 原问题 启发的 : 谷歌:我们90%的工程师使用您编写的软件(Homebrew),但是您却无法在面试时在白板上写出翻转二叉树这道题,这太糟糕了. 思路 二叉树问题,我们首先要想到的使用递归的方式来解决,有了这个思路,处理这道问题就很简单了:先互换根节…
/* * @lc app=leetcode.cn id=111 lang=c * * [111] 二叉树的最小深度 * * https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/description/ * * algorithms * Easy (37.27%) * Total Accepted: 12.2K * Total Submissions: 32.6K * Testcase Example: '[3,9,20,nu…
/* * @lc app=leetcode.cn id=104 lang=c * * [104] 二叉树的最大深度 * * https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/description/ * * algorithms * Easy (67.34%) * Total Accepted: 33.2K * Total Submissions: 49.2K * Testcase Example: '[3,9,20,nu…
/* * @lc app=leetcode.cn id=101 lang=c * * [101] 对称二叉树 * * https://leetcode-cn.com/problems/symmetric-tree/description/ * * algorithms * Easy (45.30%) * Total Accepted: 23.8K * Total Submissions: 52.4K * Testcase Example: '[1,2,2,3,4,4,3]' * * 给定一个二叉…
题目不难,很快ac,纯粹靠手感.https://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ /** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { private…
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: void serch_patch(TreeNode* root, TreeNode* node,vecto…
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { private: int res = INT_MIN; int getMax(TreeNode* r) { if(r ==…