[LeetCode] Binary Tree Paths - 二叉树基础系列题目
生活随笔
收集整理的這篇文章主要介紹了
[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
判斷兩顆二叉樹是否相等,非遞歸方法通過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;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:
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學雜了容易混亂,再次驗證了學精的重要性。 (By:Eastmount 2015-9-9 凌晨1點 ??http://blog.csdn.net/eastmount/)
與50位技術專家面對面20年技術見證,附贈技術全景圖
總結
以上是生活随笔為你收集整理的[LeetCode] Binary Tree Paths - 二叉树基础系列题目的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [LeetCode] Add Digit
- 下一篇: [LeetCode] First Bad