data structure -- tree

1. 层序遍历:

  • 递归方法:

    • 先遍历特定层。 level 指的是第几层,从1开始。只打印当level == 1的节点,其余全部忽略
    • void print_single_level(TNode* root, int level)
      {
          if (!root)
              return;
          if (level == 1)
          {
              cout << root->val << ",";
          }
          else
          {
              print_single_level(root->left, level-1);
              print_single_level(root->right, level-1);
          };
      };


    • 然后遍历所有层。
    • int get_height(TNode* root)
      {
          if (!root)
              return 0;
          if (!root->left && !root->right)
              return 1;
          return max(get_height(root->left), get_height(root->right)) + 1;
      };
      
      void print_by_level(TNode* root, vector<int>& result)
      {
          int height = get_height(root);
          for(int i = height; i >= 1; --i)
          {
              print_single_level(root, height-i+1);
              cout << endl;
          }
      };


  • 非递归方法:queue
  • 1. init queue (root, end_point)
    2. while (queue.size() > 0)
    {
        tmp_node = queue.front();
        cout << tmp->node->val;
        if (tmp_node != end_point)
        {
            queue.push_back(tmp_node->left);
            queue.push_back(tmp_node->right);
        }
        else
        {
            cout << endl;
            if (queue.size() > 0)
                queue.push_back(end_point);
        }
        
    }


  • void print_tree(TNode* root)
    {
        // print the tree level by level
        queue<TNode*> result;
        if (!root) return;
        result.push(root);
        result.push(newNode(INT_MIN));
        while ( result.size() > 0)
        {
            TNode* tmp = result.front();
            result.pop();
            if (tmp->val == INT_MIN)
            {
                if (result.size() <=0) break;
                result.push(newNode(INT_MIN));
                cout << endl;
                continue;
            } cout << tmp->val << ",";
            if (tmp->left)
                result.push(tmp->left);
            if (tmp->right)
                result.push(tmp->right);
        }
    };


2. 中序遍历
  • 非递归
  • 1) Create an empty stack S.
    2) Initialize current node as root
    3) Push the current node to S and set current = current->left until current is NULL
    4) If current is NULL and stack is not empty then 
         a) Pop the top item from stack.
         b) Print the popped item, set current = popped_item->right 
         c) Go to step 3.
    5) If current is NULL and stack is empty then we are done.
    void non_recurise_print_inorder(TNode* root, vector<int>& result)
    {
        if (!root) return;
        stack<TNode*> tmp_stack;
        TNode* current = root;
        while(1)
        {
            if (current)
            {
                tmp_stack.push(current);
                current = current->left;
            }
            else
            {
                if (tmp_stack.size() > 0)
                {
                    current = tmp_stack.top();
                    tmp_stack.pop();
                    cout << current->val << ",";
                    current = current->right;
                }
                else
                    break;
            }
        }
        
    };


3. 计算Tree 的直径: (计算方式就是叶子节点之间的最长路径,大小为经过路径的最多的节点数目)

思路:max( left_tree's diameter, right_tree's diameter, max(left_height, right_height) + 1)

int tree_diameter(TNode* root, int&height)
{
    int diameter = 0;
    if (!root)
    {
        height = 0;
        return 0;
    }
    int lheight,rheight;
    int l_dia = tree_diameter(root->left, lheight);
    int r_dia = tree_diameter(root->right, rheight);
    height = max(lheight,rheight) + 1;
    return max(max(l_dia, r_dia), lheight+rheight+1);
};

4. 根据中序遍历和先序遍历恢复binary tree

思路:递归恢复

TNode* create_tree_by_two_order(TNode* root, vector<int> preorder, vector<int>inorder, int pre_start, int pre_end, int in_start, int in_end)
{
    // find the root node
    if (pre_start > pre_end || in_start > in_end)
        return NULL;
    if (pre_start == pre_end || in_start == in_end)
    {
        return newNode(preorder[pre_start]);
    }
    int index = in_start;
    
    for (index = in_start; index < in_end && inorder[index] != preorder[pre_start]; index++)
        ;
    root = newNode(preorder[pre_start]);
    root->left = create_tree_by_two_order(root, preorder, inorder, pre_start+1, pre_start + index - in_start , in_start, index-1);
    root->right = create_tree_by_two_order(root, preorder, inorder, pre_start+index-in_start+1, pre_end, index+1, in_end);
    return root;
    
};



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值