[LeetCode] Binary Tree Paths - 二叉树基础系列题目
2023-09-14 08:56:52 时间
题目概述:
Given a binary tree, return all root-to-leaf paths.
For example, given the following binary tree:
题目解析:
本题主要考察二叉树遍历操作,输出二叉树的所有路径,通常采用递归方法能很好的解决。但是如果采用C语言编写,返回二维字符串数组如何添加二叉树路径是个难点?
char** binaryTreePaths(struct TreeNode* root, int* returnSize) {}
最终采用C++完成,当遍历至叶子节点时,通过容器push_back添加一条路径。
我的代码:
推荐代码:
Java代码 地址:http://segmentfault.com/a/1190000003465753
判断两颗二叉树是否相等,非递归方法通过isSameNode依次遍历结点
题目概述:
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
题目解析:
判断二叉树是否为镜像对称二叉树,当时错误理解为判断完全二叉树。解题思路是通过比较左右结点,左结点- left和右结点- right比较、左结点- right和右结点- left比较。
非递归算法可以采用层次遍历,每次比较同一层的数是否镜像即可。
我的代码:
非递归代码:
来源地址:http://blog.csdn.net/lc_910927/article/details/36180075
PS:二叉树是面试中经常考察的题目,包括建立二叉树、遍历二叉树、二叉树交换、二叉树求和等。希望文章对你有所帮助,同时Java、C#、C++、C学杂了容易混乱,再次验证了学精的重要性。
LeetCode 110. Balanced Binary Tree 给定一颗二叉树,判断此树是不是平衡二叉树. 平衡二叉树的条件是:当前节点的左右子树高度差不超过1,且左右子树都是平衡二叉树.
# Leetcode 67:Add Binary(二进制求和) Leetcode 67:Add Binary(二进制求和) (python、java) Given two binary strings, return their sum (also a binary string). The input strings are both non-empty and contains only characters 1 or 0. 给定两个二进制字符串,返回他们的和(用二进制表示)。
Given a binary tree, return all root-to-leaf paths.
For example, given the following binary tree:
1All root-to-leaf paths are:
["1- 2- 5", "1- 3"]
题目解析:
本题主要考察二叉树遍历操作,输出二叉树的所有路径,通常采用递归方法能很好的解决。但是如果采用C语言编写,返回二维字符串数组如何添加二叉树路径是个难点?
char** binaryTreePaths(struct TreeNode* root, int* returnSize) {}
最终采用C++完成,当遍历至叶子节点时,通过容器push_back添加一条路径。
我的代码:
/** * 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: //创建空容器 对象类型为string类 vector string result; void getPaths(TreeNode* node,string path) { if(node- left==NULL node- right==NULL) { //左右子树为空 路径寻找完成 增加至数组中 result.push_back(path); if(node- left!=NULL) { //递归遍历左子树 当前路径添加左孩子结点 getPaths(node- left,path+"- "+to_string(node- left- val)); if(node- right!=NULL) { //递归遍历右子树 getPaths(node- right,path+"- "+to_string(node- right- val)); //获取二叉树路径 vector string binaryTreePaths(TreeNode* root) { if(root==NULL) return result; getPaths(root, to_string(root- val)); //to_string整数转换为字符串 return result; };
推荐代码:
Java代码 地址:http://segmentfault.com/a/1190000003465753
public class Solution { List String res = new ArrayList String public List String binaryTreePaths(TreeNode root) { if(root != null) findPaths(root,String.valueOf(root.val)); return res; private void findPaths(TreeNode n, String path){ if(n.left == null n.right == null) res.add(path); if(n.left != null) findPaths(n.left, path+"- "+n.left.val); if(n.right != null) findPaths(n.right, path+"- "+n.right.val); }Same Tree
判断两颗二叉树是否相等,非递归方法通过isSameNode依次遍历结点
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; //递归方法 bool isSameTree(struct TreeNode* p, struct TreeNode* q) { if(p==NULL q==NULL) return true; else if( (p!=NULL q==NULL) || (p==NULL q!=NULL) ) return false; else if(p- val != q- val) return false; else return isSameTree(p- left, q- left) isSameTree(p- right, q- right); }
题目概述:
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
1 / \ / \ 3 4 4 3But the following is not:
1 3 3Note: Bonus points if you could solve it both recursively and iteratively.
题目解析:
判断二叉树是否为镜像对称二叉树,当时错误理解为判断完全二叉树。解题思路是通过比较左右结点,左结点- left和右结点- right比较、左结点- right和右结点- left比较。
非递归算法可以采用层次遍历,每次比较同一层的数是否镜像即可。
我的代码:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; //比较左右结点 bool isSameNode(struct TreeNode* L, struct TreeNode* R) { if(L==NULL R==NULL) { return true; else if((L!=NULL R==NULL) || (L==NULL R!=NULL)) { //其中一个为空 return false; else if(L- val!=R- val) { return false; else { return isSameNode(L- left,R- right) isSameNode(L- right,R- left); //判断二叉树是否为镜像对称 bool isSymmetric(struct TreeNode* root) { if(!root) return true; else { return isSameNode(root- left,root- right); }
非递归代码:
来源地址:http://blog.csdn.net/lc_910927/article/details/36180075
class Solution { public: bool isSymmetric (TreeNode* root) { if (!root) return true; stack TreeNode* s.push(root- left); s.push(root- right); while (!s.empty ()) { auto p = s.top (); s.pop(); auto q = s.top (); s.pop(); if (!p !q) continue; if (!p || !q) return false; if (p- val != q- val) return false; s.push(p- left); s.push(q- right); s.push(p- right); s.push(q- left); return true; };
PS:二叉树是面试中经常考察的题目,包括建立二叉树、遍历二叉树、二叉树交换、二叉树求和等。希望文章对你有所帮助,同时Java、C#、C++、C学杂了容易混乱,再次验证了学精的重要性。
LeetCode 110. Balanced Binary Tree 给定一颗二叉树,判断此树是不是平衡二叉树. 平衡二叉树的条件是:当前节点的左右子树高度差不超过1,且左右子树都是平衡二叉树.
# Leetcode 67:Add Binary(二进制求和) Leetcode 67:Add Binary(二进制求和) (python、java) Given two binary strings, return their sum (also a binary string). The input strings are both non-empty and contains only characters 1 or 0. 给定两个二进制字符串,返回他们的和(用二进制表示)。
相关文章
- Java实现 LeetCode 810 黑板异或游戏 (分析)
- Java实现 LeetCode 671 二叉树中第二小的节点(遍历树)
- Java实现 LeetCode 643 子数组最大平均数 I(滑动窗口)
- Java实现 LeetCode 606 根据二叉树创建字符串(遍历树)
- Java实现 LeetCode 404 左叶子之和
- Java实现 LeetCode 329 矩阵中的最长递增路径
- Java实现 LeetCode 222 完全二叉树的节点个数
- Java实现 LeetCode 145 二叉树的后序遍历
- Java实现 LeetCode 111 二叉树的最小深度
- Java实现 LeetCode 110 平衡二叉树
- Java实现 LeetCode 94 二叉树的中序遍历
- Java实现 LeetCode 91 解码方法
- Java实现 LeetCode 16 最接近的三数之和
- leetcode 41 缺失的第一个正数
- 【二叉树】LeetCode 105. 从前序与中序遍历序列构造二叉树【中等】
- 【数组&双指针】leetcode 234. 回文链表【简单】
- LeetCode: 107_Binary Tree Level Order Traversal II | 二叉树自底向上的层次遍历 | Easy
- LeetCode(102):二叉树的层次遍历
- LeetCode-2335. 装满杯子需要的最短总时长【贪心,数学】
- ( “树” 之 BFS) 637. 二叉树的层平均值 ——【Leetcode每日一题】
- ( “树” 之 DFS) 111. 二叉树的最小深度 ——【Leetcode每日一题】
- ( “树” 之 DFS) 104. 二叉树的最大深度 ——【Leetcode每日一题】
- 【LeetCode 简单 树 python3】543. 二叉树的直径
- [LeetCode] 543. 二叉树的直径 ☆(递归、数最大深度)
- 【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
- 【3】无重复字符的最长子串 【LeetCode】