[LeetCode] Binary Tree Paths - 二叉树基础系列题目
目录:
1.Binary Tree Paths - 求二叉树路径
2.Same Tree - 判断二叉树相等
3.Symmetric Tree - 判断二叉树对称镜像
Binary Tree Paths
题目概述:
Given a binary tree, return all root-to-leaf paths.
For example, given the following binary tree:
1/ \ 2 3\5
All 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
/*** 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;elsereturn isSameTree(p->left, q->left) && isSameTree(p->right, q->right);}
}
Symmetric Tree
题目概述:
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/ \2 2/ \ / \ 3 4 4 3
1/ \2 2\ \3 3
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; 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] Binary Tree Paths - 二叉树基础系列题目相关推荐
- [LeetCode] Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1/ ...
- 【easy】257. Binary Tree Paths 二叉树找到所有路径
http://blog.csdn.net/crazy1235/article/details/51474128 花样做二叉树的题--居然还是不会么-- /*** Definition for a bi ...
- LeetCode : Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- [LeetCode] Binary Tree Level Order Traversal 二叉树层次遍历(DFS | BFS)
目录: 1.Binary Tree Level Order Traversal - 二叉树层次遍历 BFS 2.Binary Tree Level Order Traversal II - 二叉树层次 ...
- LeetCode刷题记录14——257. Binary Tree Paths(easy)
LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...
- Binary Tree Paths leetcode
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...
- CF750G New Year and Binary Tree Paths(数位dp二进制+数学)
CF750G New Year and Binary Tree Paths description solution code description 题目链接 一颗无穷个节点的完全二叉树. 求有多少 ...
- Binary Tree Paths
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...
- LeetCode 257. Binary Tree Paths (二叉树路径)
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1/ ...
最新文章
- Oracle_052_lesson_p10
- 如何创建 Code Snippet
- python opencv实时显示测量数据_python OpenCV 宽度测量
- linux / 命令行 / LD_DEBUG 命令
- Win2003环境下简单的安全配置
- Linux 命令之 touch -- 创建文件
- idea 启动界面导入项目_如何为您的项目启动有效的登录页面
- 微信开发之网页授权获取用户基本信息
- windows 系统 oracle监听无法启动。
- 字典写入excel_Excel中“先出式”出货的问题,以后出库太方便了
- 迅雷精简版 4.0.0 Mac中文版
- Windows下 OpenCV 的下载安装教程(详细)
- Java 编写的 坦克大战小游戏
- ps cs6更新服务器无响应,photoshop cs6打开无响应或者不能打开图片文件最全解决办法...
- 关闭小米系统自动更新通知
- 从前慢-深入理解JVM-篇章2
- 性能功能LocustJmeter LoadRunner优缺点
- 香港理工大学酒店管理html,香港理工大学大酒店管理硕士要求
- Android面试复习资料整理
- Ancient Cipher C++题解