LeetCode—Binary Tree Right Side View 二叉树层序遍历变形,Flatten Binary Tree to Linked List前序遍历变形

本文详细介绍了如何通过层序遍历解决二叉树右视图问题,并阐述了如何将二叉树扁平化为链表。包括算法实现、代码解析及应用实例。

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.

For example:
Given the following binary tree,

   1            <---
 /   \
2     3         <---
 \     \
  5     4       <---

You should return [1, 3, 4].

https://leetcode.com/problems/binary-tree-right-side-view/ 题目位置

其实就是层序遍历,然后记录每一层中节点,最后一个的值

经常做到和层序相关的题目,之前是一个二叉树的Z扫描的问题,和这里基本上才用了一样的数据结构,所以很熟悉,写完就AC。

利用了一个vector存储每一层的元素,然后进行一个简单的递归 

如果二叉树中没有对每一行有特别需要的要求,那么就是可以直接利用 队列,如果有要求,也可以在队列中加入一个类似NULL节点表示结束

http://blog.csdn.net/xietingcandice/article/details/44939919 


/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> res;
    vector<int> rightSideView(TreeNode *root) { //其实就是每一行的最后一个节点
        res.clear();
        if(root == NULL)
        {
            return res;
        }
        vector<TreeNode*> node;
        node.push_back(root);
        GetNum(node);
        return res;
    }
    void GetNum(vector<TreeNode*> &node)
    {
        if(node.empty())
        {
            return;
        }
        vector<TreeNode*>pNode;
        //<获取当前层最右边节点的值
        res.push_back(node[node.size()-1]->val);
        //<按顺序获取他的子节点
        for(int i = 0; i < node.size();i++)
        {
            TreeNode * root = node[i];
            if(root->left)
            {
                pNode.push_back(root->left);
            }
            if(root->right)
            {
                pNode.push_back(root->right);
            }
        }
        GetNum(pNode);
    }
};

Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place.

For example,
Given

         1
        / \
       2   5
      / \   \
     3   4   6

The flattened tree should look like:
   1
    \
     2
      \
       3
        \
         4
          \
           5
            \
             6
其实就是一个前序遍历的题目:
/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<TreeNode*> pNode;
    void flatten(TreeNode *root) { //<看着就是一个前序遍历
        if(root == NULL)
        {
            return;
        }
        PreOrder(root);
        for(int i = 1; i < pNode.size(); i++)
        {
            root->left = NULL;
            root->right = pNode[i];
            root = root->right;
        }
        root->left = NULL;
        root->right = NULL;
    }
    void PreOrder(TreeNode *root)
    {
        if(root == NULL)
        {
            return;
        }
        pNode.push_back(root);
        if(root->left)
        {
            PreOrder(root->left);
        }
        if(root->right)
        {
            PreOrder(root->right);
        }
    }
};



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值