13ms to solve this problem


  • 0
    E

    /**

    • 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<int> boundaryOfBinaryTree(TreeNode
      root) {
      vector<int> vv;
      if(root==NULL){
      return vv;
      }
      stack<TreeNode*> ss;
      vv.push_back(root->val);
      if(root->left!=NULL){
      ss.push(root->left);
      }
      while(!ss.empty()){
      TreeNode* tn= ss.top();
      ss.pop();
      if(tn->left==NULL&&tn->right == NULL){
      vv.push_back(tn->val);
      break;
      }
      vv.push_back(tn->val);
      if(tn->right!=NULL){
      ss.push(tn->right);
      }
      if(tn->left != NULL){
      ss.push(tn->left);
      }
      }
      while(!ss.empty()){
      TreeNode* tn= ss.top();
      ss.pop();
      if(tn->left==NULL&&tn->right == NULL){
      vv.push_back(tn->val);
      }
      if(tn->right!=NULL){
      ss.push(tn->right);
      }
      if(tn->left != NULL){
      ss.push(tn->left);
      }
      }
      stack<int> sss;
      while(!ss.empty()){
      ss.pop();
      }
      if(root->right!=NULL){
      ss.push(root->right);
      }
      while(!ss.empty()){
      TreeNode* tn= ss.top();
      ss.pop();
      if(tn->left==NULL&&tn->right == NULL){
      sss.push(tn->val);
      break;
      }
      sss.push(tn->val);
      if(tn->left != NULL){
      ss.push(tn->left);
      }
      if(tn->right!=NULL){
      ss.push(tn->right);
      }
      }
      while(!ss.empty()){
      TreeNode* tn= ss.top();
      ss.pop();
      if(tn->left==NULL&&tn->right == NULL){
      sss.push(tn->val);
      }
      if(tn->left != NULL){
      ss.push(tn->left);
      }
      if(tn->right!=NULL){
      ss.push(tn->right);
      }
      }
      while(!sss.empty()){
      int i = sss.top();
      sss.pop();
      vv.push_back(i);
      }
      return vv;
      }
      };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.