From cd886f0db9a37c5658fdb0e9d567d769f367f871 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: Fri, 23 Jun 2017 01:31:23 -0500 Subject: [PATCH] Create 545.Boundary of Binary Tree.cpp --- .../545.Boundary of Binary Tree.cpp | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 Tree/545.Boundary-of-Binary-Tree/545.Boundary of Binary Tree.cpp diff --git a/Tree/545.Boundary-of-Binary-Tree/545.Boundary of Binary Tree.cpp b/Tree/545.Boundary-of-Binary-Tree/545.Boundary of Binary Tree.cpp new file mode 100644 index 000000000..4647ebe72 --- /dev/null +++ b/Tree/545.Boundary-of-Binary-Tree/545.Boundary of Binary Tree.cpp @@ -0,0 +1,85 @@ +/** + * 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: + vector boundaryOfBinaryTree(TreeNode* root) + { + vectorresults; + if (root==NULL) return results; + + results.push_back(root->val); + + vectorLeft; + if (root->left!=NULL) + { + GoLeft(root->left,Left); + Left.pop_back(); + } + + vectorBottom; + if (root->left!=NULL || root->right!=NULL) + GoBottom(root,Bottom); + + vectorRight; + if (root->right!=NULL) + { + GoRight(root->right,Right); + Right.pop_back(); + } + reverse(Right.begin(),Right.end()); + + for (int i=0; i& Left) + { + if (root==NULL) return; + + Left.push_back(root->val); + if (root->left!=NULL) + GoLeft(root->left,Left); + else if (root->right!=NULL) + GoLeft(root->right,Left); + } + + void GoRight(TreeNode* root, vector& Right) + { + if (root==NULL) return; + + Right.push_back(root->val); + if (root->right!=NULL) + GoRight(root->right,Right); + else if (root->left!=NULL) + GoRight(root->left,Right); + } + + void GoBottom(TreeNode* root, vector& Bottom) + { + if (root==NULL) return; + if (root->left==NULL && root->right==NULL) + { + Bottom.push_back(root->val); + } + else + { + GoBottom(root->left,Bottom); + GoBottom(root->right,Bottom); + } + } +};