N叉树问题

在二叉树的问题基础上,扩展到了N叉树,但是方法运用类似

需要解决N叉树的遍历问题,常见的有前序遍历和后序遍历,还有N叉树的最大深度问题

给定一个 n 叉树的根节点  root ,返回 其节点值的 前序遍历 ,后序遍历

前序遍历:递归

/**
 * Definition for a Node.
 * struct Node {
 *     int val;
 *     int numChildren;
 *     struct Node** children;
 * };
 */

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
#define MAX_NODE_SIZE 10000
 void helper(const struct Node*root,int *res,int *pos)
 {
    if(root==NULL)
    return;
    res[(*pos)++]=root->val;
    for(int i=0;i<root->numChildren;i++)
    {
        helper(root->children[i],res,pos);
    }
 }
int* preorder(struct Node* root, int* returnSize) {
    int* res=(int*)malloc(sizeof(int)*MAX_NODE_SIZE);
    int pos=0;
    helper(root,res,&pos);
    *returnSize=pos;
    return res;
}

后序遍历:递归

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
void postorder(struct TreeNode* root, int* res, int* resSize)
{
    if (root == NULL)
        return;
    
    postorder(root->left, res, resSize);
    postorder(root->right, res, resSize);
   res[(*resSize)++] = root->val;
    
}

int* postorderTraversal(struct TreeNode* root, int* returnSize) {
    int* res = malloc(sizeof(int) * 2000);
    *returnSize = 0;
    postorder(root, res, returnSize);
    return res;
}

 

给定一个 N 叉树,找到其最大深度。

最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。

解:与二叉树一样,采用深度优先搜索和递归解决

/**
 * Definition for a Node.
 * struct Node {
 *     int val;
 *     int numChildren;
 *     struct Node** children;
 * };
 */

int maxDepth(struct Node* root) {
    if(root==NULL)
    return 0;
    int maxChildDepth=0;
    for(int i=0;i<root->numChildren;i++)
    {
        maxChildDepth=fmax(maxChildDepth,maxDepth(root->children[i]));

    }
    return maxChildDepth+1;
}

在此提供python的写法

 

class Node:
    def __init__(self, val=None, children=None):
        self.val = val
        self.children = children
"""

class Solution:
    def maxDepth(self, root: 'Node') -> int:
        if root==None:
            return 0
        else:
            return max((self.maxDepth(child) for child in root.children),default=0)+1
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个基于回溯法和N的解决N皇后问题C语言实现。 N皇后问题是指,在一个N*N的棋盘上放置N个皇后,使得皇后之间不会互相攻击,即在同一行、同一列或同一斜线上不会存在两个及以上的皇后。 以下是代码实现: ``` #include <stdio.h> #include <stdlib.h> #define N 8 // N皇后问题的规模 #define MAX_SIZE N*N int board[N][N] = {0}; // 棋盘,0表示无皇后,1表示有皇后 int solution[MAX_SIZE][2] = {0}; // 存储解的数组 int sol_count = 0; // 解的数量 // 判断在row行col列放置皇后是否合法 int is_valid(int row, int col) { // 检查同一列上是否有皇后 for (int i = 0; i < row; i++) { if (board[i][col] == 1) { return 0; } } // 检查左上方是否有皇后 for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) { if (board[i][j] == 1) { return 0; } } // 检查右上方是否有皇后 for (int i = row - 1, j = col + 1; i >= 0 && j < N; i--, j++) { if (board[i][j] == 1) { return 0; } } // 否则该位置合法 return 1; } // 回溯求解 void solve(int row) { if (row == N) { // 找到一个解 for (int i = 0; i < N; i++) { solution[sol_count][i] = board[i][0]; // 记录解 } sol_count++; return; } for (int col = 0; col < N; col++) { if (is_valid(row, col)) { // 如果该位置合法 board[row][col] = 1; // 放置皇后 solve(row + 1); // 继续往下一行寻找 board[row][col] = 0; // 恢复该位置状态 } } } int main() { solve(0); // 从第0行开始寻找解 printf("总共找到%d个解:\n", sol_count); for (int i = 0; i < sol_count; i++) { printf("解%d:", i+1); for (int j = 0; j < N; j++) { printf("(%d,%d) ", j, solution[i][j]); } printf("\n"); } return 0; } ``` 上面的代码中,我们定义了一个8*8的棋盘,使用0表示无皇后,1表示有皇后。在求解过程中,我们使用回溯法在棋盘上依次尝试放置皇后,直到找到一个合法的解为止。在每次找到一个解后,我们将这个解存储到solution数组中,最后输出所有解。 希望这个代码能够解决您的问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值